package cn.edu.lzu.userweb.realm;

import cn.edu.lzu.entity.User;
import cn.edu.lzu.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

/**
 * @PackageName: cn.edu.lzu.userweb.realm
 * @ClassName: UserRealm
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/3/19 11:41
 * @Version: 1.0
 */
public class UserRealm extends AuthorizingRealm {
    @Resource
    private UserService userService;


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        User user=userService.getUserByUserIdentity((String)authenticationToken.getPrincipal());
        if(user==null){
            throw new AuthenticationException("账号不存在！");
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(user, user.getPassword(), null, this.getName());
        return simpleAuthenticationInfo;
    }
}
